﻿import QtQuick 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Private 1.0
import QtQuick.Window 2.2
Control {
    id: slider

    signal openSliver()
    signal endSliver()
    signal restart()
     property real step: (slider.maximumValue - slider.minimumValue)/(range.positionAtMaximum - range.positionAtMinimum)
    //年月日时分秒
    property  int endTimeStr :0;
    //是否开启拖动
    property  bool dropMouse :true
    //总的
    property  var mothTimeStr : new Array(0);
    //标签
    property  var endMothTimeStr : new Array(0);
    //中间的值
    property  var endHourTimeStr : new Array(0);
    //循环几次
    property  int endInt : 0;
    //传递的次数
     property  var zhongTimeStr : new Array(0);
    //测试定时器时间
    property  int testTimer : 10;
    //类型
    property bool loValue: false
    //滑块的布局方向 是枚举类型
    property int orientation: Qt.Horizontal
//    property bool jubSta: false
    //滑块的最小值 默认是0.0
    property alias minimumValue: range.minimumValue

    // 该属性保存滑块的最大值 默认是1.0
    property alias maximumValue: range.maximumValue

    //  此属性指示是否应当更新当前值
    //   用户正在移动滑块手柄，或仅当按钮被释放时。
    //    例如，如果更改滑块值会变化，则可以修改此属性
    //    出来太费时间了。

    //    默认值为true。
    property bool updateValueWhileDragging: true
    //    该属性指示滑块手柄是否被按下
    readonly property alias pressed: mouseArea.pressed
    //此属性指示滑动柄是否处于悬停状态。

    readonly property alias hovered: mouseArea.handleHovered

   //此属性指示滑块步长。
    property alias stepSize: range.stepSize
    //此属性保存滑块的当前值
    property alias value: range.value

//   此属性指示滑块是否应在活动时接收焦点。
    property bool activeFocusOnPress: false

    //此属性指示滑块是否应显示标记
//    以间隔时间。 刻度标记间距是根据stepSizes属性
    property bool tickmarksEnabled: true

    /*! \internal */
    property bool __horizontal: orientation === Qt.Horizontal

    /*! \internal
        The extra arguments positionAtMinimum and positionAtMaximum are there to force
        re-evaluation of the handle position when the constraints change (QTBUG-41255).
    */
    property real __handlePos: range.valueForPosition(__horizontal ? fakeHandle.x : fakeHandle.y, range.positionAtMinimum, range.positionAtMaximum)

    activeFocusOnTab: true

    Accessible.role: Accessible.Slider
    /*! \internal */
    function accessibleIncreaseAction() {
        range.increaseSingleStep()
    }
    /*! \internal */
    function accessibleDecreaseAction() {
        range.decreaseSingleStep()
    }

//    style: Qt.createComponent(Settings.style + "/SliderStyle.qml", slider)
    style: Qt.createComponent("SliderStyleMy.qml",slider)
    Keys.onRightPressed: if (__horizontal) range.increaseSingleStep()
    Keys.onLeftPressed: if (__horizontal) range.decreaseSingleStep()
    Keys.onUpPressed: if (!__horizontal) range.increaseSingleStep()
    Keys.onDownPressed: if (!__horizontal) range.decreaseSingleStep()


    RangeModel {
        id: range
        minimumValue: 0.0
        maximumValue: 1.0
        value: 0
        stepSize: 0.0
        inverted: __horizontal ? false : true
        positionAtMinimum: 0
        positionAtMaximum: __horizontal ? slider.width - fakeHandle.width : slider.height - fakeHandle.height
    }
    //动的位置
    Item {
        id: fakeHandle
        anchors.verticalCenter: __horizontal ? parent.verticalCenter : undefined
        anchors.horizontalCenter: !__horizontal ? parent.horizontalCenter : undefined
        width: __panel.handleWidth
        height: __panel.handleHeight
        function updatePos() {
            if (updateValueWhileDragging && !mouseArea.drag.active)
            {
                range.position = __horizontal ? x  : y
            }
        }

        onXChanged: updatePos();
        onYChanged: updatePos();
    }

    MouseArea {
        id: mouseArea
        visible: dropMouse
        anchors.fill: parent
//        hoverEnabled: Settings.hoverEnabled
         hoverEnabled: Settings.hoverEnabled
        property int clickOffset: 0
        property real pressX: 0
        property real pressY: 0
        property bool handleHovered: false

        function clamp ( val ) {
            return Math.max(range.positionAtMinimum, Math.min(range.positionAtMaximum, val))
        }

        function updateHandlePosition(mouse, force) {
            var pos, overThreshold
            if (__horizontal) {
                pos = clamp (mouse.x + clickOffset - fakeHandle.width/2)
                overThreshold = Math.abs(mouse.x - pressX) >= Settings.dragThreshold
                if (overThreshold)
                    preventStealing = true
                if (overThreshold || force)
                    fakeHandle.x = pos
            } else if (!__horizontal) {
                pos = clamp (mouse.y + clickOffset- fakeHandle.height/2)
                overThreshold = Math.abs(mouse.y - pressY) >= Settings.dragThreshold
                if (overThreshold)
                    preventStealing = true
                if (overThreshold || force)
                    fakeHandle.y = pos
            }
        }

        onPositionChanged: {
            if (pressed)
                updateHandlePosition(mouse, preventStealing)

            var point = mouseArea.mapToItem(fakeHandle, mouse.x, mouse.y)
            handleHovered = fakeHandle.contains(Qt.point(point.x, point.y))
        }

        onPressed: {
            if (slider.activeFocusOnPress)
                slider.forceActiveFocus();

            if (handleHovered) {
                var point = mouseArea.mapToItem(fakeHandle, mouse.x, mouse.y)
                clickOffset = __horizontal ? fakeHandle.width/2 - point.x : fakeHandle.height/2 - point.y
            }
            pressX = mouse.x
            pressY = mouse.y
            updateHandlePosition(mouse, !Settings.hasTouchScreen)
            emit : onEndSliver
        }

        onReleased: {
            updateHandlePosition(mouse, Settings.hasTouchScreen)
            // If we don't update while dragging, this is the only
            // moment that the range is updated.
            if (!slider.updateValueWhileDragging)
                range.position = __horizontal ? fakeHandle.x  : fakeHandle.y;

            clickOffset = 0
            preventStealing = false
             emit : openSliver
        }

        onExited: handleHovered = false
    }


    // During the drag, we simply ignore the position set from the range, this
    // means that setting a value while dragging will not "interrupt" the
    // dragging activity. 当不在鼠标拖动时起作用
    Binding {
        when: !mouseArea.drag.active
        target: fakeHandle
        property: __horizontal ? "x" : "y"
        value: range.position
    }
    Timer
    {
        id:timeRon002
        interval: 1000; running: false; repeat: true
        onTriggered:
            {

//            timeRon.start()
//            value =0
//            endInt ++;
//            timeRon002.stop()
//            jubSta = false
        }

    }

    Timer
    {
        id:timeRon
        interval: 10; running: false; repeat: true
        onTriggered:
        {

            if(value >= maximumValue)
            {
                if(endInt <= (endTimeStr -1) )
                {
                    loValue= true
//                    timeRon.stop()
//                    jubSta = true
//                    timeRon002.start()

                    value =0
                    endInt ++;
                }
                else
                {
                    timeRon.stop()
                    emit:xiugaiSliverImage(1)

                }

            }else
            {
                value = value +0.01
                if(endInt*7 >=zhongTimeStr.length-1)
                {
                    var y = parseInt(Math.floor(value*100)/100) +(endInt-1)*7
                    if(y===zhongTimeStr.length-1)
                    {
                         timeRon.stop()
                    }
                }

            }


        }
    }

    function getArray(x,y,z)
    {
        endTimeStr = x

        if(mothTimeStr.length >0)
        {
            mothTimeStr =[]
        }
        if(endMothTimeStr.length >0)
        {
            endMothTimeStr =[]
        }
        if(endHourTimeStr.length >0)
        {
            endHourTimeStr =[]
        }
        mothTimeStr = y
        zhongTimeStr = z
        var tempendMothTimeStr = new Array(0)
        var tempendHourTimeStr = new Array(0)
        for(var i = 0 ; i<7 ;i++)
        {
            tempendMothTimeStr.push(mothTimeStr[i])
            tempendHourTimeStr.push(mothTimeStr[i])
        }
        endMothTimeStr = tempendMothTimeStr
        endHourTimeStr = tempendHourTimeStr
        endInt =1;
        value = 0
        timeRon.stop()
        emit:xiugaiSliverImage(0)
    }

    Connections
    {
        onOpenSliver:
        {
//            if( jubSta === true)
//            {
//                timeRon002.start()
//            }else
//            {
//                timeRon.start()
//            }
            timeRon.start()

        }
        onEndSliver:
        {
//            if( jubSta === true)
//            {
//                timeRon002.stop()
//            }else
//            {
//                timeRon.stop()
//            }
             timeRon.stop()

        }
        onRestart:
        {
            value =0
            endInt = 0
//            if( jubSta === true)
//            {
//                timeRon002.stop()
//                jubSta = false
//            }else
//            {
//                timeRon.stop()
//            }
            timeRon.stop()
            loValue = false

            if(mothTimeStr.length===0)
            {

            }else
            {
                if(updatePro === true)
                {
                      mdata.text = slider.zhongTimeStr[0].slice(0,10).replace("-",".").replace("-",".")
                }else
                {
                     mtime.text = slider.zhongTimeStr[0]
                }

                var tempendMothTimeStr = new Array(0)
                var tempendHourTimeStr = new Array(0)
                for(var i = 0 ; i<7 ;i++)
                {
                     tempendMothTimeStr.push(mothTimeStr[i])

                    tempendHourTimeStr.push(mothTimeStr[i])
                }
                endMothTimeStr = tempendMothTimeStr
                endHourTimeStr = tempendHourTimeStr
                endInt = 1;
            }


        }
    }
}
